<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('销售订单主列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>订单编号：</label>
                                <input type="text" name="orderCode"/>
                            </li>
                            <li>
                                <label>客户公司：</label>
                                <input type="text" name="customerName"/>
                            </li>
                            <li>
                                <label>商品名称：</label>
                                <input type="text" name="productName"/>
                            </li>
                            <li>
                                <label>商品类别：</label>
                                <select name="categoryId" th:with="type=${@dict.getType('product_category')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>销售日期：</label>
                                <input type="text" class="time-input" placeholder="请选择销售日期" name="orderDate"/>
                            </li>
                            <li>
                                <label>预计交货日期：</label>
                                <input type="text" class="time-input" placeholder="请选择预计交货日期" name="expectedDeliveryDate"/>
                            </li>
                            <li>
                                <label>订单总金额：</label>
                                <input type="text" name="totalAmount"/>
                            </li>
                            <li>
                                <label>订单状态：</label>
                                <select name="orderStatus" th:with="type=${@dict.getType('sales_order_status')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>审核状态：</label>
                                <select name="auditStatus" th:with="type=${@dict.getType('sales_audit_status')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>创建者：</label>
                                <input type="text" name="createdBy"/>
                            </li>
                            <li>
                                <label>创建时间：</label>
                                <input type="text" class="time-input" placeholder="请选择创建时间" name="createdTime"/>
                            </li>
                            <li>
                                <label>更新者：</label>
                                <input type="text" name="updatedBy"/>
                            </li>
                            <li>
                                <label>更新时间：</label>
                                <input type="text" class="time-input" placeholder="请选择更新时间" name="updatedTime"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="sales:order:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="sales:order:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="sales:order:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="sales:order:export">
                    <i class="fa fa-download"></i> 导出
                </a>
                <a class="btn btn-info multiple disabled" onclick="batchExecute()" shiro:hasPermission="sales:execution:add">
                    <i class="fa fa-play"></i> 批量执行
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('sales:order:edit')}]];
        var removeFlag = [[${@permission.hasPermi('sales:order:remove')}]];
        var executeFlag = [[${@permission.hasPermi('sales:execution:add')}]];
        var orderStatusDatas = [[${@dict.getType('sales_order_status')}]];
        var auditStatusDatas = [[${@dict.getType('sales_audit_status')}]];
        var productCategoryDatas = [[${@dict.getType('product_category')}]];
        var prefix = ctx + "sales/order";
        var executionPrefix = ctx + "sales/execution";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "销售订单主",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'orderId',
                    title: '订单ID',
                    visible: false
                },
                {
                    field: 'orderCode',
                    title: '订单编号'
                },
                {
                    field: 'customerName',
                    title: '客户公司'
                },
                {
                    field: 'productName',
                    title: '商品名称'
                },
                {
                    field: 'categoryId',
                    title: '商品类别',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(productCategoryDatas, value);
                    }
                },
                {
                    field: 'quantity',
                    title: '销售数量'
                },
                {
                    field: 'unitPrice',
                    title: '销售单价'
                },
                {
                    field: 'orderDate',
                    title: '销售日期'
                },
                {
                    field: 'expectedDeliveryDate',
                    title: '预计交货日期'
                },
                {
                    field: 'totalAmount',
                    title: '订单总金额'
                },
                {
                    field: 'orderStatus',
                    title: '订单状态',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(orderStatusDatas, value);
                    }
                },
                {
                    field: 'auditStatus',
                    title: '审核状态',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(auditStatusDatas, value);
                    }
                },
                {
                    field: 'auditOpinion',
                    title: '审核意见'
                },
                {
                    field: 'remark',
                    title: '备注'
                },
                {
                    field: 'createdBy',
                    title: '创建者'
                },
                {
                    field: 'createdTime',
                    title: '创建时间'
                },
                {
                    field: 'updatedBy',
                    title: '更新者'
                },
                {
                    field: 'updatedTime',
                    title: '更新时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.orderId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.orderId + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        
                        // 只要审核通过的订单都可以执行，不再限制订单状态
                        if (row.auditStatus == '1') {
                            actions.push('<a class="btn btn-info btn-xs ' + executeFlag + '" href="javascript:void(0)" onclick="executeOrder(\'' + row.orderId + '\')"><i class="fa fa-play"></i>执行</a>');
                        }
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
        
        /* 执行销售订单 */
        function executeOrder(orderId) {
            $.modal.open("执行销售订单", executionPrefix + "/execute/" + orderId, '800', '300');
        }
        
        /* 批量执行销售订单 */
        function batchExecute() {
            var rows = $.table.selectFirstColumns();
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            
            // 打开批量执行页面
            var url = executionPrefix + "/executeBatch?orderIds=" + rows.join();
            $.modal.open("批量执行销售订单", url, '800', '350');
        }
    </script>
</body>
</html>